<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Proxy 与 Reflect</title>
</head>
<body>
  <script>
    const user = {
      name: 'tom',
      age: 12
    }

    // 创建一个代理对象
    const proxy = new Proxy(user, {
      // 读取属性
      get(target, property, receiver) {
        console.log('监听到读取属性', property)
        // 通过Reflect读取目标对象中对应的属性值
        return Reflect.get(target, property, receiver)
      },
      // 设置或添加属性
      set (target, property, value, receiver) {
        console.log('监听到设置或添加属性', property, value)

        
        // 通过Reflect给目标对象设置或添加属性
        return Reflect.set (target, property, value, receiver)
      },
      // 删除属性
      deleteProperty (target, property) {
        console.log('监听到删除属性', property)
        return Reflect.deleteProperty(target, property) 
      }
    })

    proxy.name = 'Jack'

    console.log(proxy.name)

    // 添加一个新属性
    proxy.sex = '男'

    delete proxy.sex

    console.log(user)




  </script>
</body>
</html>